package com.example.shushiyunsst.fragment.main.wallet

import android.Manifest
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseDataBindingFragment
import com.example.library_common.global.AppGlobals
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.utils.even.TO_REFRESH_BALANCE_INFO
import com.example.library_common.view.bottombar.BottomBorInfo
import com.example.shushiyunsst.R
import com.example.shushiyunsst.databinding.WalletFragmentLayoutBinding
import com.example.shushiyunsst.fragment.getUserBalanceInfo
import com.example.shushiyunsst.fragment.main.wallet.sub.*
import com.example.wallet.bean.BalanceInfoBean
import com.example.wallet.db.BalanceInfoManager
import com.example.wallet.db.UserInfoManager
import kotlinx.coroutines.*
import org.greenrobot.eventbus.EventBus

class WalletFragment : BaseDataBindingFragment<WalletFragmentLayoutBinding>(), BottomBorInfo,
    CoroutineScope by MainScope() {

    companion object {
        fun newInstance(): WalletFragment {
            val args = Bundle()

            val fragment = WalletFragment()
            fragment.arguments = args
            return fragment
        }

    }

    override var icon = R.drawable.ic_wallet


    override var title = R.string.wallet

    override fun layoutId() = R.layout.wallet_fragment_layout

    private var mPagerAdapter:FragmentStatePagerAdapter?=null

    private val subFragments = arrayOf<Fragment>(
//        MyCaptureFragment.newInstance(),
        MyNewCaptureFragment.newInstance(),
        MarketFragment.newInstance()
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport=false
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        loadRootFragment(R.id.wallet_header, WalletHeadFragment.newInstance())

        mPagerAdapter= object
            :
            FragmentStatePagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

            override fun getCount() = subFragments.size

            override fun getItem(position: Int) = subFragments[position]

            override fun getPageTitle(position: Int): CharSequence? {
                val walletLabInfo = getItem(position) as WalletLabInfo
                return AppGlobals.getApplication().getString(walletLabInfo.title)
            }

        }

        viewBinder.mViewPager.adapter = mPagerAdapter

        viewBinder.mTabLayout.setupWithViewPager(viewBinder.mViewPager)


    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        if (mPagerAdapter !=null){
            refreshData()
        }
    }

    override fun refreshData() {
        val currentItem = viewBinder.mViewPager.currentItem
        val walletLabInfo =  mPagerAdapter?.getItem(currentItem) as WalletLabInfo
        walletLabInfo.refreshData()
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)

        getUserBalanceInfo(_mActivity)
    }


    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }


}
